//
//  PreviewImageVC.m
//  dyseller
//
//  Created by 红喇叭 on 2017/11/22.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "PreviewImageVC.h"

NSNotificationName const FoodimageUpdate = @"FoodimageUpdate";
@interface PreviewImageVC ()<UIActionSheetDelegate,UIImagePickerControllerDelegate>

@property(nonatomic,strong)UIImageView *previewImg;

@end

@implementation PreviewImageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    //self.view.backgroundColor = kBlackColor;
    if(_imagePath){
        [self.previewImg sd_setImageWithURL:[NSURL URLWithString:_imagePath] placeholderImage:nil];
    }else{
        self.previewImg.image = [ToolClass getDocumentImage:ImagePath];
    }
    
    
    
    UIButton *savebtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    savebtn.titleLabel.font = font14;
    [savebtn setTitle:@"重新上传" forState:0];
    [savebtn setTitleColor:kGrayColor forState:0];
    [savebtn addTarget:self action:@selector(chooseImage) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:savebtn];

}

- (UIImageView *)previewImg{
    if(_previewImg == nil){
        _previewImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_W)];
        _previewImg.centerY = self.view.centerY-NavigationHeight/2;
        _previewImg.contentMode = UIViewContentModeScaleAspectFit;
        [self.view addSubview:_previewImg];
    }
    return _previewImg;
}

#pragma  mark ========选择相片========
- (void)chooseImage{
    UIActionSheet *sheet;
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从相册选择", nil];
    }else{
        sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"从相册选择", nil];
    }
    sheet.tag = 225;
    [sheet showInView:self.view];
}

#pragma  mark ========UIActionSheetDelegate========
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(actionSheet.tag == 225){
        NSUInteger sourceType = 0;
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
            switch (buttonIndex) {
                case 0:
                    //取消
                    return;
                    break;
                case 1:
                    //相机
                    sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                case 2:
                    //相册
                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
                default:
                    break;
            }
        }else{
            if(buttonIndex==0){
                return;
            }else{
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            }
        }
        
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.sourceType = sourceType;
        imagePickerController.navigationBar.translucent = NO;
        imagePickerController.allowsEditing = YES;
        [self presentViewController:imagePickerController animated:YES completion:nil];
    }
}

#pragma  mark ========UIImagePickerControllerDelegate========
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
    UIImage *newImg = [info objectForKey:UIImagePickerControllerEditedImage];
   
    [[PPHttpRequsetData shareInstance] uploadImages:Request_POST URL:@"/ShopApi/Public/imgUpload" isactive:NO imageData:@[newImg] imgfileName:@"img_file" parameter:@{@"img_path":@"GOODS/"} successBlock:^(PPHttpRequest *request, id responseObj) {
        [_previewImg sd_setImageWithURL:responseObj[@"url"] placeholderImage:nil];
        [userDefaults setObject:responseObj[@"url"] forKey:@"HLBImageUrl"];
        [[NSNotificationCenter defaultCenter] postNotificationName:FoodimageUpdate object:nil];

    } failBlock:^(PPHttpRequest *request, NSString *errMsg) {

    }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}


@end
